﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Perovich.GameObjects.Attributes;
using System.Diagnostics;
using Microsoft.Xna.Framework.Graphics;

namespace Perovich.GameObjects.Components
{
    public enum BoundingStyle
    {
        /// <summary>
        /// The entity will wrap to the other side of the border.
        /// </summary>
        Wrap,
        /// <summary>
        /// The entity will bounce off the edges of the border.
        /// </summary>
        Bounce
    }


    public struct Bounds
    {
        public int Left { get; private set; }
        public int Right { get; private set; }
        public int Top { get; private set; }
        public int Bottom { get; private set; }
        public Bounds(int right, int left, int top, int bottom)
            :this()
        {
            Right = right;
            Left = left;
            Top = top;
            Bottom = bottom;
        }
    }

    [ComponentDependency(typeof(Velocity2D), typeof(Size2D))]
    [AutomaticProperty("Bounds", "Bounds2D.Bounds")]
    [AutomaticProperty("Right", "Bounds2D.Right")]
    [AutomaticProperty("Left", "Bounds2D.Left")]
    [AutomaticProperty("Top", "Bounds2D.Top")]
    [AutomaticProperty("Bottom", "Bounds2D.Bottom")]
    [AutomaticProperty("BoundingStyle", "Bounds2D.BoundingStyle")]
    public sealed class Bounds2D : EntityComponent
    {
        public Bounds Bounds
        {
            get;
            set;
        }

        public int Top
        {
            get
            {
                return Bounds.Top;
            }
            set
            {
                Bounds = new Bounds(Bounds.Right, Bounds.Left, value, Bounds.Bottom);
            }
        }

        public int Bottom
        {
            get
            {
                return Bounds.Bottom;
            }
            set
            {
                Bounds = new Bounds(Bounds.Right, Bounds.Left, Bounds.Top, value);
            }
        }

        public int Left
        {
            get
            {
                return Bounds.Left;
            }
            set
            {
                Bounds = new Bounds(Bounds.Right, value, Bounds.Top, Bounds.Bottom);
            }
        }

        public int Right
        {
            get
            {
                return Bounds.Right;
            }
            set
            {
                Bounds = new Bounds(value, Bounds.Left, Bounds.Top, Bounds.Bottom);
            }
        }

        public BoundingStyle BoundingStyle
        {
            get;
            set;
        }

        public override void Initialize()
        {
            if ((Top == 0) && (Bottom == 0) && (Left == 0) && (Right == 0))
            {
                GraphicsDevice dev = ((IGraphicsDeviceService)this.Owner.Parent.Services.GetService(typeof(IGraphicsDeviceService))).GraphicsDevice;
                Bottom = dev.Viewport.Height;
                Right = dev.Viewport.Width;
            }
            base.Initialize();
        }
    }
}
